package com.jus.player.activity.my;

import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.common.util.CollectionUtils;
import com.jus.player.R;
import com.jus.player.activity.BaseActivity;
import com.jus.player.activity.home.JsPlayerActivity;
import com.jus.player.adapter.my.HistoryAdapter;
import com.jus.player.config.AppConfig;
import com.jus.player.dao.AppDatabase;
import com.jus.player.dao.WatchHistoryDao;
import com.jus.player.entity.WatchHistory;
import com.jus.player.util.CommonUtils;
import com.jus.player.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 观看历史页面
 */
public class HistoryActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private View emptyView;
    private HistoryAdapter adapter;

    private WatchHistoryDao mHistoryDao;
    private static long userId;
    @Override
    protected int initLayout() {
        return R.layout.activity_history;
    }

    @Override
    protected void initView() {
        //返回按钮
        RelativeLayout back = findViewById(R.id.system_back);
        back.setOnClickListener(view -> finish());

        // 初始化数据库
        mHistoryDao = AppDatabase.getInstance(this).watchHistoryDao();

        recyclerView = findViewById(R.id.recyclerView_history);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new HistoryAdapter(this, new ArrayList<>());
        recyclerView.setAdapter(adapter);

        //空页面
        emptyView = findViewById(R.id.layout_empty_state_history);
        adapter.setOnItemClickListener(new HistoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(WatchHistory history) {
                JsPlayerActivity.start(HistoryActivity.this,
                        history.getEpisode() - 1, history.getVideoId(), false);
            }

            @Override
            public void onDeleteClick(WatchHistory history) {
                deleteHistory(history);
            }
        });
    }

    @Override
    protected void initData() {
        String userIdStr = CommonUtils.findFromSp(this, AppConfig.SP_USER_ID);
        userId = StringUtils.isEmpty(userIdStr) ? 0 : Long.parseLong(userIdStr);
        loadData();
        //搜索热门视频
        //getSearchHotVideos();
    }

    private void loadData() {
        new Thread(() -> {
            List<WatchHistory> histories;
            synchronized (AppConfig.historyLock) {
                histories = mHistoryDao.getAllByUserId(userId);
            }
            runOnUiThread(() -> {
                if (CollectionUtils.isEmpty(histories)) {
                    // 没有数据时显示空状态页面，隐藏列表
                    recyclerView.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                } else {
                    adapter.updateData(histories);
                }
            });
        }).start();
    }

    private void deleteHistory(WatchHistory history) {
        new Thread(() -> {
            mHistoryDao.deleteByVideoIdAndUserId(history.getVideoId(), userId);
            loadData(); // 刷新列表
        }).start();
    }

    @Override
    protected void onResume() {
        loadData();
        super.onResume();
        getWindow().setStatusBarColor(Color.WHITE);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        );
    }

    @Override
    protected void onPause() {
        super.onPause();
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        );
    }

}